package com.kerrykidz.parreserve.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.ORDER_CONSTANTS;
import com.kerrykidz.dao.model.LocPartyInoutEntity;
import com.kerrykidz.filemanagement.dao.mapper.LocPartyInoutEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusMemEntityMapper;
import com.kerrykidz.membership.dao.model.CusMemEntity;
import com.kerrykidz.parreserve.dao.mapper.GblParCWricorEntityMapper;
import com.kerrykidz.parreserve.dao.mapper.ParCTypeEntityMapper;
import com.kerrykidz.parreserve.dao.mapper.ParReserveEntityMapper;
import com.kerrykidz.parreserve.dao.mapper.ParReserveRoomEntityMapper;
import com.kerrykidz.parreserve.dao.mapper.ParRoomEntityMapper;
import com.kerrykidz.parreserve.dao.model.GblParCWricorEntity;
import com.kerrykidz.parreserve.dao.model.ParCTypeEntity;
import com.kerrykidz.parreserve.dao.model.ParReserveEntity;
import com.kerrykidz.parreserve.dao.model.ParReserveRoomEntity;
import com.kerrykidz.parreserve.dao.model.ParRoomEntity;
import com.kerrykidz.parreserve.service.IParReserveService;
import com.kerrykidz.promotion.dao.mapper.ProCampaignEntityMapper;
import com.kerrykidz.promotion.dao.model.ProCampaignEntity;
import com.kerrykidz.sales.dao.mapper.PosOrderEntityMapper;
import com.kerrykidz.sales.dao.mapper.PosOrderPrdEntityMapper;
import com.kerrykidz.sales.dao.model.PosOrderEntity;
import com.kerrykidz.sales.dao.model.PosOrderPrdEntity;
import com.kerrykidz.scminvoicing.dao.mapper.GblScmPrdEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdPriceEntity;
import com.kerrykidz.system.dao.mapper.GblSecBranEntityMapper;
import com.kerrykidz.system.dao.mapper.SecUsrEntityMapper;
import com.kerrykidz.system.dao.mapper.SysBarCodeEntityMapper;
import com.kerrykidz.system.dao.mapper.SysHolidayEntityMapper;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.dao.model.SecUsrEntity;
import com.kerrykidz.system.dao.model.SysBarCodeEntity;
import com.kerrykidz.system.dao.model.SysHolidayEntity;
import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.JXLSUtil;
import com.rdp.framework.core.model.MyBatisSuperModel;
import com.rdp.framework.exception.RDPException;

@Service
public class ParReserveServiceImpl implements IParReserveService {
	@Autowired
	SecUsrEntityMapper usrMapper;
	@Autowired
	private ParRoomEntityMapper parRoomEntityMapper;
	@Autowired
	private ParReserveEntityMapper parReserveEntityMapper;
	@Autowired
	private GblScmPrdEntityMapper gblScmPrdEntityMapper;
	@Autowired
	private ParReserveRoomEntityMapper parReserveRoomEntityMapper;
	@Autowired
	private ProCampaignEntityMapper proCampaignEntityMapper;
	@Autowired
	private ParCTypeEntityMapper parCTypeEntityMapper;
	@Autowired
	private CusMemEntityMapper memMapper;
	@Autowired
	private SecUsrEntityMapper secUsrEntityMapper;
	@Autowired
	private PosOrderEntityMapper posOrderEntityMapper;
	@Autowired
	private PosOrderPrdEntityMapper posOrderPrdEntityMapper;
	@Autowired
	private LocPartyInoutEntityMapper locPartyInoutEntityMapper;
	@Autowired
	private GblParCWricorEntityMapper gblParCWricorEntityMapper;
	@Autowired
	private SysBarCodeEntityMapper sysBarCodeEntityMapper;
	@Autowired
	private SysHolidayEntityMapper sysHolidayEntityMapper;
	@Autowired
	private GblSecBranEntityMapper gblSecBranEntityMapper;
	
	public List<ParRoomEntity> findParRoom(String branId) {
		return parRoomEntityMapper.findParRoom(branId);
	}

	public int insertParReserve(ParReserveEntity parReserve) {
		if("0".equals(parReserve.getGrpFlag())){//0 派对
//			if(StringUtils.isNotBlank(parReserve.getChdName())){
//				try {//手动输入 的会员姓名 ，判断此姓名的客户 是否 为 会员
//					CusMemEntity cusMem=findMemberByMemName(parReserve.getChdName());
//					if(null ==cusMem || !cusMem.getPkMemId().equals(parReserve.getFkMemId())||"".equals(parReserve.getFkMemId())){
//						parReserve.setFkMemId("");
//					}else  if(!"".equals(parReserve.getFkMemId())){
//						parReserve.setFkMemId(parReserve.getFkMemId());
//					}
//				} catch (RDPException e) {
//					e.printStackTrace();
//				}
//				
//			}
		}else if("1".equals(parReserve.getGrpFlag())){//1 团体 也没有 会员id//1 团体 也没有 会员id
			parReserve.setChdName("");
			parReserve.setFkMemId(null);
		}
		 if("".equals(parReserve.getFkOtprdId())){
			 parReserve.setFkOtprdId(null);
		 }
		return  parReserveEntityMapper.insertParReserve(parReserve);
		
	}

	public List<GblScmPrdEntity> findParReserveTaocan(GblScmPrdEntity parReserve) {
		parReserve.setIspaging(true);
		return gblScmPrdEntityMapper.findParReserveTaocan(parReserve);
	}

	public List<GblScmPrdEntity> findParReserveTaocanByCateId(GblScmPrdEntity entity) throws RDPException {
		return gblScmPrdEntityMapper.findProductList(entity);
	}
	public List<GblScmPrdEntity> findParReserveDandian(GblScmPrdEntity parReserve){
		parReserve.setIspaging(true);
		return parReserveEntityMapper.findParReserveFujia(parReserve);
	}

	public List<GblScmPrdEntity> findParReserveTrimming(GblScmPrdEntity parReserve){
		parReserve.setIspaging(true);
		return parReserveEntityMapper.findParReserveFujia(parReserve);
	}

	public int updateParReserve(ParReserveEntity parReserveEntity) {
		boolean flagUpdatefkMemId =false;
		if("0".equals(parReserveEntity.getGrpFlag())){//0 派对
//			if(StringUtils.isNotBlank(parReserveEntity.getChdName())){
//				try {//手动输入 的会员姓名 ，判断此姓名的客户 是否 为 会员
//					CusMemEntity cusMem=findMemberByMemName(parReserveEntity.getChdName());
//					if(null ==cusMem || !cusMem.getPkMemId().equals(parReserveEntity.getFkMemId()) ||"".equals(parReserveEntity.getFkMemId())){
//						parReserveEntity.setFkMemId("");
//						flagUpdatefkMemId=true;
//					}else  if(!"".equals(parReserveEntity.getFkMemId())){
//						parReserveEntity.setFkMemId(parReserveEntity.getFkMemId());
//					}
//				} catch (RDPException e) {
//					e.printStackTrace();
//				}
//			}
		}else if("1".equals(parReserveEntity.getGrpFlag())){//1 团体 也没有 会员id
			parReserveEntity.setChdName("");
			parReserveEntity.setFkMemId(null);
			flagUpdatefkMemId=true;
		}
		
		if(StringUtils.isNotBlank(parReserveEntity.getPkParresId())){
			ParReserveEntity parReserveOld =  findParReserveById(parReserveEntity.getPkParresId());
			if(null !=parReserveOld ){
				if(parReserveOld.getResStatus().equals(Constants.RESERVE_STATUS_CONFIRMED) 
						|| StringUtils.isNotBlank(parReserveOld.getFkPriorresId())){
					String presDayNew =DateUtil.formatDate("yyyy-MM-dd", parReserveEntity.getResDay());
					String presDayOld =DateUtil.formatDate("yyyy-MM-dd", parReserveOld.getResDay());
					if(!presDayNew.equals(presDayOld)){
						 Integer timecount= parReserveOld.getModiTimeCnt();
						if(null ==timecount){
							timecount =0;
						}
						parReserveEntity.setModiTimeCnt(timecount+1);
					}
				}
			
			}
		}
		if(flagUpdatefkMemId){
		 parReserveEntityMapper.updateParresFkmemId(parReserveEntity);
		}
		return parReserveEntityMapper.updateParReserve(parReserveEntity);
	}

	public List<GblScmPrdEntity> findScmRrdByprdIdList(
			Map<String, Object> map) {
		return gblScmPrdEntityMapper.findScmRrdByprdIdList(map);
	}

	public List<GblScmPrdEntity> findScmRrdByprdIdListPage(
			GblScmPrdEntity parReserve) {
		parReserve.setIspaging(true);
		return gblScmPrdEntityMapper.findScmRrdByprdIdListPage(parReserve);
	}

	public ParReserveEntity findParReserveById(String pkParresId) {
		ParReserveEntity parReserveEntity =new ParReserveEntity();
		parReserveEntity.setPkParresId(pkParresId);
		return parReserveEntityMapper.findParReserveById(parReserveEntity);
	}

	public int insertPReserveRoom(ParReserveRoomEntity parReserveRoom) {
		return parReserveRoomEntityMapper.insertParReserveRoom(parReserveRoom);
	}

	public List<ParReserveRoomEntity> findParReserveRoomByparId(String pkParresId) {
		ParReserveRoomEntity parReserveRoom=new ParReserveRoomEntity();
				parReserveRoom.setFkParresId(pkParresId);
				List<ParReserveRoomEntity> parReserveRoomList=	parReserveRoomEntityMapper.findParReserveRoomByparId(parReserveRoom);
		return parReserveRoomList;
	}
	
	public List<ParReserveEntity> parReservePageListManages(ParReserveEntity parReserve){
		parReserve.setIspaging(true);
		return parReserveEntityMapper.parReservePageListManages(parReserve);
	}
	
	public List<ParReserveEntity> findParReserveRooMListView(ParReserveEntity parReserve){
		return parReserveEntityMapper.findParReserveRooMList(parReserve);
	}

	public int deleteParRoomByParId(String pkParresId) {
		ParReserveRoomEntity parReserveRoom =new ParReserveRoomEntity();
		parReserveRoom.setFkParresId(pkParresId);
		return parReserveRoomEntityMapper.deleteParRoomByParId(parReserveRoom);
	}

	public List<PosOrderPrdEntity> pageOrderPrdListByOrdId(String fkBranId,String pkOrderId) {
//		posOrderPrd.setIspaging(true);
		List<PosOrderPrdEntity> posOrderPrdList=parReserveEntityMapper.pageOrderPrdListByOrdId(fkBranId,pkOrderId);
		return posOrderPrdList;
	}

	public List<GblScmPrdPriceEntity> findPrdPriceByPrdId(
			GblScmPrdPriceEntity prdPrice) {
		return parReserveEntityMapper.findPrdPriceByPrdId(prdPrice);
	}

	public double findPrdPriceByPrdId(String prdId) {
		List<GblScmPrdPriceEntity> prdPriceList =new ArrayList<GblScmPrdPriceEntity>();
		GblScmPrdPriceEntity prdPrice =new GblScmPrdPriceEntity();
		prdPrice.setFkPrdId(prdId);
		prdPriceList=parReserveEntityMapper.findPrdPriceByPrdId(prdPrice);
		double headPrice=0.00;
		if(null!=prdPriceList&& prdPriceList.size()>0){
			prdPrice=prdPriceList.get(0);
		    headPrice =prdPrice.getHeadPrice().doubleValue();
		} 
		return headPrice;
	}

	public  List<CusMemEntity> findMemberByNameOrTel(CusMemEntity cusMem) throws RDPException{
		cusMem.setIspaging(true);
		List<CusMemEntity> cusMemEntityList =parReserveEntityMapper.findMemberByPhoneAndName(cusMem);
		return cusMemEntityList;
	}

	public List<ParReserveEntity> findParReserve(String branId,
			String resStatus, String grpFlag) {
		ParReserveEntity reserveEntity=new ParReserveEntity();
		reserveEntity.setGrpFlag(grpFlag);
		reserveEntity.setResStatus(resStatus);
		reserveEntity.setFkBranId(branId);
		return parReserveEntityMapper.findParReserve(reserveEntity);
	}

	public List<ParReserveEntity> findAlreadyRoomList(
			ParReserveEntity parReserve) {
		return parReserveEntityMapper.findAlreadyRoomList(parReserve);
	}

	public List<ParCTypeEntity> findParCtypeList() {
		return parCTypeEntityMapper.findParCtypeList();
	}

	public List<String> findParReserveDateRooM(ParReserveEntity parReserveEntity) {
		return parReserveEntityMapper.findParReserveDateRooM(parReserveEntity);
	}

	public List<PosOrderEntity> findPosOrderByParresId(String parresId) {
		return posOrderEntityMapper.findByParresId(parresId);
	}

	public List<ParRoomEntity> findParRoomOrderby() {
		return parRoomEntityMapper.findParRoomOrderby();
	}

 

	public List<SysBarCodeEntity> findSysBarCodeList(SysBarCodeEntity sysBarCode) {
		return parReserveEntityMapper.findSysBarCodeList(sysBarCode);
	}

	public void addLocParTyInOut(LocPartyInoutEntity locPartyInout) {
		locPartyInoutEntityMapper.addLocParTyInOut(locPartyInout);
		
	}

	public void updateLocParTyInOut(LocPartyInoutEntity locPartyInout) {
		locPartyInoutEntityMapper.updateLocParTyInOut(locPartyInout);
		
	}


	public void deleteLocParTyInOut(LocPartyInoutEntity locPartyInout) {
		locPartyInoutEntityMapper.deleteLocParTyInOut(locPartyInout);
	}
	
	public void deleteLocParTyInOutById(LocPartyInoutEntity locPartyInout) {
		locPartyInoutEntityMapper.deleteLocParTyInOutById(locPartyInout);
	}

	public List<GblParCWricorEntity> findParWricorList() {
		return gblParCWricorEntityMapper.findParWricorList();
	}

	public void deleteLocParTyInOutByParresId(String fkParresId) {
		locPartyInoutEntityMapper.deleteLocParTyInOutByParresId(fkParresId);
		
	}

	public List<ParReserveEntity> getAllPartyInOut(MyBatisSuperModel model) {
		model.setIspaging(true);
		return locPartyInoutEntityMapper.getAllPartyInOut(model);
	}


	public List<LocPartyInoutEntity> localPartyInCodeList(
			LocPartyInoutEntity locPartyInout) {
		return locPartyInoutEntityMapper.findLocParTyInList(locPartyInout);
	}

	public List<LocPartyInoutEntity> findLocParTyInOut(
			LocPartyInoutEntity locPartyInout) {
		return locPartyInoutEntityMapper.findLocParTyInOut(locPartyInout);
	}

	public GblParCWricorEntity findParWricorById(String pkWricorId) {
		return gblParCWricorEntityMapper.findParWricorById(pkWricorId);
	}

	
	public void updateParReserveFKCancel() {
		List<String> parresIdList=parReserveEntityMapper.findParReserveFkCancel();
		if(null !=parresIdList && parresIdList.size()>0){
			for(String parresId:parresIdList){
				PosOrderEntity order =new PosOrderEntity();
				order.setFkParresId(parresId);
				PosOrderEntity posOrder = posOrderEntityMapper.findByCondition(order);
				if(null !=posOrder){
					posOrder.setOrderStatus(ORDER_CONSTANTS.ORDER_STATUS_CANCEL);
					posOrder.setModiTime(new Date());
					posOrderEntityMapper.updateByPrimaryKeySelective(posOrder);
				}
			}
			parReserveEntityMapper.updateParReserveFkCancel();
		}
	}
	
	public void updateParReserveAutoCancel(String dateTime) {
		List<String> parresIdList=parReserveEntityMapper.findParReserveAutoCancel(dateTime);
		if(null !=parresIdList && parresIdList.size()>0){
			for(String parresId:parresIdList){
				PosOrderEntity order =new PosOrderEntity();
				order.setFkParresId(parresId);
				PosOrderEntity posOrder = posOrderEntityMapper.findByCondition(order);
				if(null !=posOrder){
					posOrder.setOrderStatus(ORDER_CONSTANTS.ORDER_STATUS_CANCEL);
					posOrder.setModiTime(new Date());
					posOrderEntityMapper.updateByPrimaryKeySelective(posOrder);
				}
			}
			parReserveEntityMapper.updateParReserveAutoCancel(dateTime);
		}
		
		//后加的 取消 72小时 团体 未预付
		  parresIdList=parReserveEntityMapper.findParTuantiReserveAutoCancel(dateTime);
		if(null !=parresIdList && parresIdList.size()>0){
			for(String parresId:parresIdList){
				PosOrderEntity order =new PosOrderEntity();
				order.setFkParresId(parresId);
				PosOrderEntity posOrder = posOrderEntityMapper.findByCondition(order);
				if(null !=posOrder){
					posOrder.setOrderStatus(ORDER_CONSTANTS.ORDER_STATUS_CANCEL);
					posOrder.setModiTime(new Date());
					posOrderEntityMapper.updateByPrimaryKeySelective(posOrder);
				}
			}
			parReserveEntityMapper.updateParTuanTiReserveAutoCancel(dateTime);
		}
	}

	@Override
	public List<SysBarCodeEntity> findSysBarCodeListByBranId(SysBarCodeEntity sysBarCode) {
		return parReserveEntityMapper.findSysBarCodeListByBranId(sysBarCode);
	}

	@Override
	public List<LocPartyInoutEntity> selectLocParTyInOutByParresId(
			String fkParresId) {
		return locPartyInoutEntityMapper.selectLocParTyInOutByParresId(fkParresId);
	}

	@Override
	public List<SysBarCodeEntity> selectSBarCodeBrandId(SysBarCodeEntity entity) {
		return sysBarCodeEntityMapper.selectSBarCodeBrandId(entity);
	}

	@Override
	public CusMemEntity findSusMemByMemId(String memId) throws RDPException {
		return (CusMemEntity)memMapper.findEntityById(memId);
	}

	@Override
	public GblScmPrdEntity findMemberProduct(GblScmPrdEntity entity)
			throws RDPException {
		return gblScmPrdEntityMapper.findMemberProduct(entity);
	}

	@Override
	public int getCountMemOrderByMemId(String fkMemId) {
		return parReserveEntityMapper.getCountMemOrderByMemId(fkMemId);
	}

	@Override
	public List<ProCampaignEntity> getCampaignForPar(String fkBranId)
			throws RDPException {
		return proCampaignEntityMapper.getCampaignForPar(fkBranId);
	}

	@Override
	public void updateParReservePkg(ParReserveEntity parReserveEntity) {
		parReserveEntityMapper.updateParReservePkg(parReserveEntity);
	}

	@Override
	public List<String> findValidHoliday() throws Exception {
		List<SysHolidayEntity> sysHolidayList = sysHolidayEntityMapper.findValidHoliday();
		List<String> disableDateAll =new ArrayList<String>();
		if(null != sysHolidayList && sysHolidayList.size()>0){
			for(SysHolidayEntity sysHoliday:sysHolidayList){
				Date startD =sysHoliday.getStartDate();
				Date endD =sysHoliday.getEndDate();
				if(null != sysHoliday.getStartDate() && null !=sysHoliday.getEndDate() ){
					List<String> disableDate=null;
				    disableDate =DateUtil.Date_start_end(startD,  endD);
				 if(null !=disableDate){
					 disableDateAll.addAll(disableDate);
				 }
				}
			}
		}
		return disableDateAll;
	}

	@Override
	public GblSecBranEntity selectBranByAbbrCode(String abbrCode) {
		return gblSecBranEntityMapper.selectBranByAbbrCode(abbrCode);
	}

	@Override
	public List<String> getRoomTime(Map<String, Object> map) {
		return parRoomEntityMapper.getRoomTime(map);
	}

	@Override
	public List<ParReserveRoomEntity> parResRoomCancelLocalFlag() {
		return parReserveRoomEntityMapper.parResRoomCancelLocalFlag();
	}

	@Override
	public int updateParReserveRoom(ParReserveRoomEntity parReserveRoom) {
		return parReserveRoomEntityMapper.updateParReserveRoom(parReserveRoom);
	}

	@Override
	public List<SecUsrEntity> listUserByRoleId(String roleId, String fkBrandId) {
		  return usrMapper.listUserByRoleId(roleId,fkBrandId);
	}

	@Override
	public PosOrderPrdEntity orderPrdByOrdIdAndPrdId(String pkOrderId,String fkPrdId) {
		return parReserveEntityMapper.orderPrdByOrdIdAndPrdId(pkOrderId, fkPrdId);
	}

	@Override
	public int deleteParRoom(ParReserveRoomEntity parReserveRoom) {
		return parReserveRoomEntityMapper.deleteParRoom(parReserveRoom);
	}

	@Override
	public  CusMemEntity  findMemberByMemName(String memName) throws RDPException {
		List<CusMemEntity> cusMemEntityList = memMapper.findMemberByMemName(memName);
		CusMemEntity cusmem =null;
		if(null !=cusMemEntityList && cusMemEntityList.size()>0){
			cusmem =cusMemEntityList.get(0);
		}
		return cusmem;
	}

	@Override
	public CusMemEntity findMemberById(String pkMemId) {
		return parReserveEntityMapper.findMemberById(pkMemId);
				
	}

	@Override
	public Integer getCountByDateTime(String pkParresId, String date,  String time) {
		return parReserveEntityMapper.getCountByDateTime(pkParresId, date, time);
	}

	public List<LocPartyInoutEntity> findLocParTyInListByPage(MyBatisSuperModel model) {
		model.setIspaging(true);
		return locPartyInoutEntityMapper.findLocParTyInListByPage(model);
	}

	public String expParSupplyPrd(List<?> list,List<String> listSheetNames) {
		String path=JXLSUtil.exportMoreSheetExcel("parSupplyTemplate",
	   			  list,  listSheetNames);
		return path;
	}

	@Override
	public List<String> findWristColorByresDate(Map<String, Object> map) {
		return parReserveEntityMapper.findWristColorByresDate(map);
	}

	@Override
	public List<String> findWColByresDateList(Map<String, Object> map) {
		return parReserveEntityMapper.findWColByresDateList(map);
	}
	
	public Integer findParReserveRoom(String pkParresId, String roomId) {
		return parReserveRoomEntityMapper.findParReserveRoom(pkParresId, roomId);
	}

	@Override
	public List<ParReserveEntity> getParReserveByDateTime(String pkParresId,
			String date, String time) {
		return parReserveEntityMapper.getParReserveByDateTime(pkParresId, date, time);
	}
}
